<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域  -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入订单编号"
            v-model="paramsInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchOrders"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data="orderList" border stripe height="550">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <el-tooltip effect="dark" content="修改送货地址" placement="top" :enterable="false">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="ShowLocationBox"></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="查看物流信息" placement="top" :enterable="false">
            <el-button type="success" icon="el-icon-location" size="mini" @click="ShowStoBox"></el-button>
          </el-tooltip>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="paramsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>

    <el-dialog title="修改送货地址" :visible.sync="addressVisible" width="50%" @close="AdressCloseHandle">
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="商品分类">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            clearable
            :expandTrigger="'hover'"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AdressCloseHandle">取 消</el-button>
        <el-button type="primary" @click="AdressCloseHandle">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="物流信息" :visible.sync="stoVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in stoData"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from "../../assets/citydata.js";
export default {
  data() {
    return {
      orderList: [],
      paramsInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      addressVisible: false,
      stoVisible: false,
      addressForm: {
        address1: "",
        address2: "",
      },
      addressFormRules: {
        address1: [
          {
            required: true,
            message: "请选择省市区",
            trigger: "blur",
          },
        ],
        address2: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur",
          },
        ],
      },
      cityData,
      stoData: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.paramsInfo,
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      } else {
        this.orderList = res.data.goods;
        this.total = res.data.total;
      }
    },
    handleSizeChange(newPageSize) {
      this.paramsInfo.pagesize = newPageSize;
      this.getOrderList();
    },
    handleCurrentChange(newPageNum) {
      this.paramsInfo.pagenum = newPageNum;
      this.getOrderList();
    },
    searchOrders() {
      this.paramsInfo.pagenum = 1;
      this.paramsInfo.pagesize = 10;
      this.getOrderList();
    },
    CascaderChange() {},
    ShowLocationBox() {
      this.addressVisible = true;
    },
    AdressCloseHandle() {
      this.$refs.addressFormRef.resetFields();
      this.addressVisible = false;
    },
    async ShowStoBox() {
      this.stoVisible = true;
      const { data: res } = await this.$http.get("kuaidi/1106975712662");
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      } else {
        this.stoData = res.data;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>